//
//  CDColumnCenterController.m
//  栏目中心Demo
//
//  Created by liaoxingchi on 16/9/23.
//  Copyright © 2016年 liaoxingchi. All rights reserved.
//

#import "CDColumnCenterController.h"
#import "Masonry.h"
#import "CDSubscribeController.h"
#import "CDColumnController.h"
#import "Addition.h"

@interface CDColumnCenterController ()<UIScrollViewDelegate>
@property (nonatomic, weak) UIScrollView *columnScrollView;
@property (nonatomic, weak) UIView *columnLine;
@property (nonatomic, weak) UIView *columnView;
@property (nonatomic, strong) NSArray *columnBtns;
@end

@implementation CDColumnCenterController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor cyanColor];
//    self.navigationItem.title = @"栏目中心";
    [self setNavBarWithTitle:@"栏目中心"];
    [self setupUI];
}
// 初始化UI
- (void)setupUI {
    // 创建控件
    UIView *columnView = [[UIView alloc] init];
    columnView.backgroundColor = [UIColor purpleColor];
    
    UIButton *subscribeBtn = [[UIButton alloc] init];
    [subscribeBtn setTitle:@"我的订阅" forState:UIControlStateNormal];
    [subscribeBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [subscribeBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    subscribeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    
    
    UIButton *allBtn = [[UIButton alloc] init];
    [allBtn setTitle:@"全部栏目" forState:UIControlStateNormal];
    [allBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [allBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    allBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    NSArray *columnBtns = @[subscribeBtn,allBtn];
    for (int i = 0; i < columnBtns.count; i++) {
        UIButton *btn = columnBtns[i];
        btn.tag = i;
        [btn addTarget:self action:@selector(columnbtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [columnView addSubview:btn];
    }
    // 滑线
    UIView *columnLine = [[UIView alloc] init];
    columnLine.backgroundColor = [UIColor yellowColor];
    [columnView addSubview:columnLine];
    
    
    [self.view addSubview:columnView];
    
    // 详情视图
    UIScrollView *columnScrollView = [[UIScrollView alloc] init];
    //设置分页
    columnScrollView.pagingEnabled = YES;
    columnScrollView.backgroundColor = [UIColor orangeColor];
    // 取消滚动条
    columnScrollView.showsVerticalScrollIndicator = NO;
    columnScrollView.showsHorizontalScrollIndicator = NO;
    // 遵守代理
    columnScrollView.delegate = self;
    // 添加视图
    [self.view addSubview:columnScrollView];
    
    CDSubscribeController *subVC = [[CDSubscribeController alloc] init];
    CDColumnController *colVC = [[CDColumnController alloc] init];
    // 添加视图到 scrollView上
    [columnScrollView addSubview:subVC.view];
    [columnScrollView addSubview:colVC.view];
    // 创建父子控制器
    [self addChildViewController:subVC];
    [self addChildViewController:colVC];
    // 告诉系统父子控制器已经建立
    [subVC didMoveToParentViewController:self];
    [colVC didMoveToParentViewController:self];
    
    // 自动布局
    [columnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(64);
        make.left.right.equalTo(self.view);
        make.height.offset(42);
    }];
    
    [subscribeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(columnView);
    }];
    [allBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(subscribeBtn);
        make.width.equalTo(subscribeBtn);
        make.left.equalTo(subscribeBtn.mas_right);
        make.right.equalTo(columnView);
    }];
    [columnLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(120);
        make.height.offset(2);
        make.bottom.equalTo(columnView);
        make.centerX.equalTo(subscribeBtn);
    }];
    // 详情界面
    [columnScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(columnView.mas_bottom);
        make.left.bottom.right.equalTo(self.view);
    }];
    [subVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.top.offset(0);
        make.width.equalTo(columnScrollView);
        make.height.equalTo(columnScrollView);
    }];
    [colVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.offset(0);
        make.left.equalTo(subVC.view.mas_right);
        make.width.height.equalTo(subVC.view);
    }];
    
    self.columnView = columnView;
    self.columnScrollView = columnScrollView;
    self.columnLine = columnLine;
    self.columnBtns = columnBtns;
}

#pragma mark - 按钮的点击事件
- (void)columnbtnClick:(UIButton *)sender {
    [self.columnScrollView setContentOffset:CGPointMake(sender.tag * self.columnScrollView.bounds.size.width, 0) animated:YES];
}

#pragma make - scrollView代理方法
// scrollView 在滑动的时候调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // 获取offset
    CGFloat offsetX = scrollView.contentOffset.x;
//    NSLog(@"%f", offsetX);
    UIButton* firstbtn = [self.columnBtns firstObject];
    UIButton* lastbtn = [self.columnBtns lastObject];
    
    // 最大值和最小值的距离
    CGFloat dis = lastbtn.center.x - firstbtn.center.x;
    
    // 获取这个范围的最小值和最大值
    CGFloat leftValue = -(dis / 2);
    CGFloat rightValue = (dis / 2);
    
    YHValue resValue = YHValueMake(leftValue, rightValue);
    YHValue conValue = YHValueMake(0, (self.columnBtns.count - 1) * self.columnScrollView.bounds.size.width);
    CGFloat res = [NSObject resultWithConsult:offsetX andResultValue:resValue andConsultValue:conValue];
    
    [self.columnLine mas_remakeConstraints:^(MASConstraintMaker* make) {
        make.width.offset(120);
        make.height.offset(2);
        make.bottom.equalTo(self.columnView);
        make.centerX.offset(res);
    }];
    
    [self.columnLine layoutIfNeeded];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - 允许多手势使用
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer
{
    return YES;
}


@end
